//
//  BaseVC.swift
//  DLuxTravel
//
//  Created by 陆涛 on 2018/11/21.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit

class BaseVC: UIViewController {
    
    var leftItemAction:(()->Void)?
    var rightItemAction:(()->Void)?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupBackItem()
    }
    
    func addLeftItem(icon:UIImage? = nil,title:String? = nil,handler:@escaping()->Void) {
        
        self.leftItemAction = handler
        var item:UIBarButtonItem? = nil
        
        if let icon = icon {
            
            item = UIBarButtonItem(image: icon, style: .plain, target: self, action: #selector(innerleftItemAction))
        } else {
            
            item = UIBarButtonItem(title: title, style: .plain, target: self, action: #selector(innerleftItemAction))
        }
        
        self.navigationItem.leftBarButtonItem = item
    }
    
    @objc private func innerleftItemAction() {
        
        self.leftItemAction?()
    }
    
    func addRightItem(icon:UIImage? = nil,title:String? = nil,handler:@escaping()->Void) {
        
        self.rightItemAction = handler
        var item:UIBarButtonItem? = nil
        
        if let icon = icon {
            
            item = UIBarButtonItem(image: icon, style: .plain, target: self, action: #selector(innerRightItemAction))
        } else {
            
            item = UIBarButtonItem(title: title, style: .plain, target: self, action: #selector(innerRightItemAction))
        }
        
        self.navigationItem.rightBarButtonItem = item
    }
    
    @objc private func innerRightItemAction() {
        
        self.rightItemAction?()
    }
}
